<?php

namespace App\Service\SiteLocation\Import;

use App\Enums\OpenShop\InvestmentPromotion\SignOrderEnum;
use App\Models\Common\AmapAreaModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Models\OpenShop\StartBusiness\OpenShopStartBusinessModel;
use App\Models\SiteLocation\CityShopFirstOrderPriceModel;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;

class CityShopFirstOrderPrice
{
    public int $total = 0;

    public int $success = 0;

    public function collection(Collection $collection, ?Command $command)
    {
        $collection->first()->map(function ($item, $row) use ($command) {
            $row = $row + 1;
            if ($row == 1) {
                return null;
            }
            // 常规内容校验
            [$city, $price] = $item;

            if ($city == null) {
                $command->info('循环次数' . $this->total);
                $command->info('成功次数' . $this->success);
                dd('执行结束');
            }

            // 根据city获得cityCode
            $cityCode = (int)AmapAreaModel::query()
                ->where('label', $city)
                ->where('level', AmapAreaModel::LEVEL_CITY)
                ->value('code');

            if (0 == $cityCode) {
                return null;
            }

            ++$this->total;
            $model = CityShopFirstOrderPriceModel::query()
                ->firstOrNew([
                    'cityCode' => $cityCode
                ]);

            $model->city  = $city;
            $model->price = $price ?: 20000;
            $model->save();
            ++$this->success;
            dump($city . ' -> ' . $cityCode);
        })->filter();
    }
}
